#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

// 用户结构体，存储用户的各种信息
typedef struct Users {
    char name[20];         // 用户昵称
    char Password[20];     // 用户密码
    char ID[9];            // 用户ID
    char Profile[100];     // 用户简介
    char Sex[10];          // 用户性别
    char NatalDay[20];     // 用户生日
    char District[100];    // 用户地区
    char School[50];       // 用户学校
    char mailbox[30];      // 用户邮箱
    char *video[100];      // 用户视频列表
    char *video_hide[100];
    char ***commentarea;   // 用户视频评论区指针
    int worksize[10];
    int likework[10];
    int lookworkcount[10];
    int like;              // 用户点赞数
    int foollow;           // 用户关注数
    char follow_list[100][20]; // 用户关注列表
    int fan;               // 用户粉丝数
    char fan_list[100][20]; // 用户粉丝列表
    int friends;           // 用户好友数
    char friiends_list[100][20]; // 用户好友列表
    int NumberOfLike;      // 用户总点赞数
}U;

int showMainMenu(U a[], int *usecount, int *login);
void showUserMenu(U a[], int usecount, int index, int op);
// 判断两个字符串是否相等
int Judgement(char password1[], char password2[]);
// 数字转换为字符串存储到ID中
void Turn(int *Prt, char id[]);
// 修改字符串内容
void Change(char *name2, char *prt);
void Usershow(U a[] , int usecount , int index);
// 选择用户所在地区
void district(U a[], int usecount, char *p2);
void Transport(const char *letter[], const char *letter1[][10], const char *transmit[], const char *transmit1[][10], int optioncount);
// 选择选项
void Select(U a[], int usecount, const char **option, int optioncount, char *p2, const char *(*option_city)[10], int option_city_count, const char *transmit[], const char *transmit1[][10]);

// 选择用户性别
void Option(char *pd);
// 输入用户生日
void Birthday(char *qd);
// 输入用户邮箱
void Mailbox(char *p3);
void WorkManage(U a[],int index);
// 上传视频作品
void Footage(char *avideo[],U a[],int usecount);
// 递归显示视频及其评论区
void Recursive(U a[], int Count, int index, int a12, int usecount, int *nuberoflike, int *Like, int *follow, int *fan, int op);
// 关注用户
void Followone1(int *follow, int *fan, int a12, int usecount, U a[]);
// 点赞视频
void Like2(int *numberoflike ,int *Like,U a[],int index,int a12);
// 发表评论
void Commentarea(U a[], int index, int a12);
// 取消关注用户
void Delete(U a[], int *follow, int *fan, int usecount);
// 比较关注列表，查找好友
void Compare(U a[], int usecount, int *friends);
// 显示用户信息
void Display(U a[], int number, int cho);
void Find1(U a[] , int usecount);
// 从文件中加载用户信息
int LoadUsersFromfile(U a[]);
// 将用户信息保存到文件中
void SaveUsersToFile(U a[], int usecount);

int main() {
    U a[100];
    int usecount = LoadUsersFromfile(a);
    int login = -1;
    int op = showMainMenu(a, &usecount, &login);

    if (op == 1) {
        showUserMenu(a, usecount, usecount, op);
    } else if (op == 2 && login != -1) {
        showUserMenu(a, usecount, login, op);
    }

    // 释放内存
    int i,l,g,h,ft;
    for ( i = 0; i < usecount; i++) {
        for (l = 0; l < 100; l++) {
            if (a[i].video[l] != NULL) {
                free(a[i].video[l]);
            }
        }
        for (ft = 0;ft < usecount;ft++){
        	if (a[i].video_hide[ft] != NULL){
        		free(a[i].video_hide[ft]);
			}
		}
        for ( g = 0; g < 10; g++) {
            if (a[i].commentarea != NULL && a[i].commentarea[g] != NULL) {
                for ( h = 0; h < 100; h++) {
                    if (a[i].commentarea[g][h] != NULL) {
                        free(a[i].commentarea[g][h]);
                    }
                }
                free(a[i].commentarea[g]);
            }
        }
        if (a[i].commentarea != NULL) {
            free(a[i].commentarea);
        }
    }

    SaveUsersToFile(a, usecount);
    return 0;
}

// 显示主菜单并处理用户选择
int showMainMenu(U a[], int *usecount, int *login) {
    int op = 0;
    int flag = 0;
    printf("        123\n        登录注册界面\n");
    do {
        printf("\n1.注册账号\n2.登录账号\n3.退出\n");
        int b;
        if (scanf("%d", &b) != 1) {
            // 处理输入非数字的情况
            while (getchar() != '\n');
            printf("输入错误，请输入数字！\n");
            continue;
        }
        while (getchar() != '\n'); // 清除缓冲区
		if (b == 1) {
            int flag1 = 0;
            do {
                printf("1.注册\n2.返回\n");
                int c;
                if (scanf("%d", &c) != 1) {
                    while (getchar() != '\n');
                    printf("输入错误，请输入数字！\n");
                    continue;
                }
                while (getchar() != '\n'); // 清除缓冲区

                if (c == 1) {
                    int flag3 = 0;
                    printf("请输入您的昵称。\n");
                    if (fgets(a[*usecount].name, sizeof(a[*usecount].name), stdin) == NULL) {
                        // 处理fgets读取失败的情况
                        perror("读取昵称失败");
                        continue;
                    }
                    a[*usecount].name[strcspn(a[*usecount].name, "\n")] = 0; // 去除换行符

                    do {
                        printf("请输入您的密码\n");
                        if (fgets(a[*usecount].Password, sizeof(a[*usecount].Password), stdin) == NULL) {
                            perror("读取密码失败");
                            continue;
                        }
                        a[*usecount].Password[strcspn(a[*usecount].Password, "\n")] = 0; // 去除换行符

                        char Password_1[20];
                        printf("请再次输入您的密码\n");
                        if (fgets(Password_1, sizeof(Password_1), stdin) == NULL) {
                            perror("再次读取密码失败");
                            continue;
                        }
                        Password_1[strcspn(Password_1, "\n")] = 0; // 去除换行符

                        int d = Judgement(a[*usecount].Password, Password_1);
                        if (d == 0) {
                            printf("两次密码不一致，请重新输入。\n");
                            continue;
                        } else if (d == 1) {
                            flag3 = 1;
                        }
                    } while (flag3 == 0);
					int count = 0;

                    if (count < 7) {
                        printf("恭喜您，注册账号成功！\n您的ID是：");
                        int number1 = *usecount;
                        printf("%d", number1);
                        int *prt;
                        prt = &number1;
                        Turn(prt, a[*usecount].ID);
                        a[*usecount].like = 0;
                        a[*usecount].fan = 0;
                        a[*usecount].foollow = 0;
                        a[*usecount].NumberOfLike = 0;
                        (*usecount)++;
                        op = 1;
                        int j,j_hide;
					    int q;
					    for (j = 0; j < 10; j++){
					        a[*usecount].video[j] = (char *)calloc(100, sizeof(char));
					        if (a[*usecount].video[j] == NULL){
					            printf ("内存分配失败\n");
					            int k;
					            for (k = 0; k < j; k++){
					                free(a[*usecount].video[k]);
					            }
					        }
					    }
					    for (j_hide = 0;j_hide < 10;j_hide++){
					    	a[*usecount].video_hide[j_hide] = (char *)calloc(100, sizeof(char));
					    	if (a[*usecount].video_hide[j_hide] == NULL){
					    		printf ("内存分配失败\n");
					            int k;
					            for (k = 0; k < j_hide; k++){
					                free(a[*usecount].video_hide[k]);
					            }
					            int k1;
					            for (k1 = 0;k1 < 10;k1++){
					            	free(a[*usecount].video[k1]);
								}
							}
						}
					    a[*usecount].commentarea = (char ***)calloc (10, sizeof(char **));
					    for (q = 0; q < 10; q++){
					        a[*usecount].commentarea[q] = (char **)calloc(100, sizeof(char *));
					        if (a[*usecount].commentarea[q] == NULL){
					            printf ("内存分配失败\n");
					            int p;
					            for (p = 0; p < q; p++){
					                free(a[*usecount].commentarea[p]);
					            }
					            free(a[*usecount].commentarea);
					        }
					        int g;
					        for (g = 0; g < 100; g++){
					            a[*usecount].commentarea[q][g] = (char *)calloc (1000, sizeof(char));
					            if (a[*usecount].commentarea[q][g] == NULL){
					                printf ("内存分配失败\n");
					                int h, l;
					                for (l = 0; l < q; l++){
					                    for (h = 0; h < g; h++){
					                        free(a[*usecount].commentarea[l][h]);
					                    }
					                    free(a[*usecount].commentarea[l]);
					                }
					                free(a[*usecount].commentarea);
					            }
					        }
						}        
                        break;
                    } else if (count >= 7) {
                        printf("验证失败\n");
                    }
                } else if (c == 2) {
                    flag1 = 1;
                }
            } while (flag1 == 0);
            flag = 1;
        } else if (b == 2) {
            printf("请输入您的ID：");
            char Id[9];
            if (fgets(Id, sizeof(Id), stdin) == NULL) {
                perror("读取ID失败");
                continue;
            }
            Id[strcspn(Id, "\n")] = 0; // 去除换行符

            printf("请输入您的密码\n");
            char password2[20];
            if (fgets(password2, sizeof(password2), stdin) == NULL) {
                perror("读取密码失败");
                continue;
            }
            password2[strcspn(password2, "\n")] = 0; // 去除换行符

            int e, f, j, k;
            for (e = 0; e < *usecount; e++) {
                j = Judgement(Id, a[e].ID);
                if (j == 1) {
                    break;
                }
            }
            for (f = 0; f < *usecount; f++) {
                k = Judgement(password2, a[f].Password);
                if (k == 1) {
                    break;
                }
            }
            if (j == 1 && k == 1) {
                op = 2;
                *login = e;
                int j,j_hide;
			    int q;
			    for (j = 0; j < 10; j++){
			        a[*login].video[j] = (char *)calloc(100, sizeof(char));
			        if (a[*login].video[j] == NULL){
			            printf ("内存分配失败\n");
			            int k;
			            for (k = 0; k < j; k++){
			                free(a[*login].video[k]);
			            }
			        }
			    }
			    for (j_hide = 0;j_hide < 10;j_hide++){
			    	a[*login].video_hide[j_hide] = (char *)calloc(100, sizeof(char));
			    	if (a[*login].video_hide[j_hide] == NULL){
			    		printf ("内存分配失败\n");
			            int k;
			            for (k = 0; k < j_hide; k++){
			                free(a[*login].video_hide[k]);
			            }
			            int k1;
			            for (k1 = 0;k1 < 10;k1++){
			            	free(a[*login].video[k1]);
						}
					}
				}
			    a[*login].commentarea = (char ***)calloc (10, sizeof(char **));
			    for (q = 0; q < 10; q++){
			        a[*login].commentarea[q] = (char **)calloc(100, sizeof(char *));
			        if (a[*login].commentarea[q] == NULL){
			            printf ("内存分配失败\n");
			            int p;
			            for (p = 0; p < q; p++){
			                free(a[*login].commentarea[p]);
			            }
			            free(a[*login].commentarea);
			        }
			        int g;
			        for (g = 0; g < 100; g++){
			            a[*login].commentarea[q][g] = (char *)calloc (1000, sizeof(char));
			            if (a[*login].commentarea[q][g] == NULL){
			                printf ("内存分配失败\n");
			                int h, l;
			                for (l = 0; l < q; l++){
			                    for (h = 0; h < g; h++){
			                        free(a[*login].commentarea[l][h]);
			                    }
			                    free(a[*login].commentarea[l]);
			                }
			                free(a[*login].commentarea);
			            }
			        }
			    }        
            } else {
                printf("ID或密码错误，请重新输入。\n");
            }
            flag = 1;
        } else if (b == 3) {
            return 0;
        } else {
            printf("输入错误，请重新输入！\n");
        }
    } while (flag == 0);
    system("cls");
    return op;
}

// 显示用户菜单并处理用户选择
void showUserMenu(U a[], int usecount, int index, int op) {
    int flag4 = 0;
    do {
        printf("1.%s\n2.%s\n3.用户简介:%s\n4.总点赞数\n5.关注\n6.粉丝\n7.好友\n8.点赞\n9.作品\n10.个人信息\n11.搜索\n12.推荐\n13.退出\n",
               a[index].name, a[index].ID, a[index].Profile);
        int f;
        scanf("%d", &f);
        while (getchar() != '\n');

        switch (f) {
            case 1: {
                system("cls");
                printf("是否修改昵称\n1.是   2.否\n");
                int a1;
                if (scanf("%d", &a1) != 1) {
                    while (getchar() != '\n');
                    printf("输入错误，请输入数字！\n");
                    continue;
                }
                while (getchar() != '\n'); // 清除缓冲区

                if (a1 == 1) {
                    char prt[20];
                    if (fgets(prt, sizeof(prt), stdin) == NULL) {
                        perror("读取新昵称失败");
                        continue;
                    }
                    prt[strcspn(prt, "\n")] = 0; // 去除换行符
                    Change(a[index].name, prt);
                    printf("修改成功\n");
                    break;
                } else if (a1 == 2) {
                    break;
                } else {
                    printf("输入错误，请重新输入。\n");
                    break;
                }
            }
            case 2: {
                system("cls");
                printf("%s\n", a[index].ID);
                break;
            }
            case 3: {
                system("cls");
                int a3 = strlen(a[index].Profile);
                if (a3 == 0) {
                    printf("简介为空，您可以进行编辑。\n");
                } else {
                    printf("是否修改简介\n1.是   2.否\n");
                    int a3_2;
                    if (scanf("%d", &a3_2) != 1) {
                        while (getchar() != '\n');
                        printf("输入错误，请输入数字！\n");
                        continue;
                    }
                    while (getchar() != '\n'); // 清除缓冲区

                    if (a3_2 == 1) {
                        char prt[100];
                        if (fgets(prt, sizeof(prt), stdin) == NULL) {
                            perror("读取新简介失败");
                            continue;
                        }
                        prt[strcspn(prt, "\n")] = 0; // 去除换行符
                        Change(a[index].Profile, prt);
                        printf("修改成功\n");
                        break;
                    } else if (a3_2 == 2) {
                        break;
                    } else {
                        printf("输入错误，请重新输入。\n");
                        break;
                    }
                }
            }
            case 4: {
                system("cls");
                printf("%d\n", a[index].NumberOfLike);
                break;
            }
            case 5: {
                system("cls");
                printf("%d\n", a[index].foollow);
                int cho = 1;
                Display(a, index, cho);
                printf("是否取消关注");
                int T;
                if (scanf("%d", &T) != 1) {
                    while (getchar() != '\n');
                    printf("输入错误，请输入数字！\n");
                    continue;
                }
                int *follow1 = &a[index].foollow;
                int *fan1 = &a[index].fan;
                if (T == 1) {
                    Delete(a, follow1, fan1, usecount);
                }
                break;
            }
            case 6: {
                system("cls");
                printf("%d\n", a[index].fan);
                int cho = 2;
                Display(a, index, cho);
                break;
            }
            case 7: {
                system("cls");
                printf("%d\n", a[index].friends);
                int *friends = &a[index].friends;
                Compare(a, usecount, friends);
                int cho = 3;
                Display(a, index, cho);
                break;
            }
            case 8: {
                system("cls");
                printf("%d\n", a[index].like);
                break;
            }
            case 9:{
                system("cls");
        		WorkManage(a,index);
                break;
            }
            case 10:{
                Usershow(a , usecount , index);
                break;
            }
            case 11:{
            	system("cls");
            	Find1(a , usecount);
				break;
			}
			case 12:{
				system("cls");
				int videoh, videoj;
				int judgementvideo = 0;
				for (videoh = 0; videoh < usecount+1; videoh++) {
				    for (videoj = 0; videoj < 10; videoj++) {
				        if (a[videoh].video[videoj] != NULL && strcmp(a[videoh].video[videoj], "") != 0) {
				            judgementvideo = 1;  // 找到有内容的视频，标记为有视频作品
				            break;
				        }
				    }
				    if (judgementvideo) {
				        break;
				    }
				}
				if (judgementvideo == 1){
	                int a12;
	                int *numberoflike;
	                int *like1;
	                int *follow2;
	                int *fan2;
	                follow2 = &a[index].foollow;
	                like1 = &a[index].like;
	                printf("asd");
	                for (a12 = 0;a12 < usecount+1;a12++){
	                    numberoflike = &a[a12].NumberOfLike;
	                    fan2 = &a[a12].fan;
	                    Recursive(a, 10 ,0 ,a12 ,index ,numberoflike ,like1 ,follow2 ,fan2 ,op);
	                }
				}else {
					printf ("没有作品\n");
				}
				break;
			}
            case 13:{
                flag4 = 1;
                break;
            }
            default:
                printf("输入错误，请重新输入。\n");
                break;
        }
    } while (flag4 == 0);
}

void Usershow(U a[] , int usecount , int index){
	system("cls");
	int a10;
    printf ("1.性别:%c\n2.生日:%s\n3.地区:%s\n4.学校:%s\n5.邮箱:%s",a[usecount].Sex,a[usecount].NatalDay,a[usecount].District,a[usecount].School,a[usecount].mailbox);
    a10 = 0;
    scanf ("%d",&a10); 
    while(getchar() != '\n');
    switch (a10){
        case 1:{
            system("cls");
            char *pd;
            pd = a[usecount].Sex;
            Option(pd);
            break;
        }
        case 2:{
            system("cls");
            char *qd;
            qd = a[usecount].NatalDay;
            Birthday(qd);
            break;
        }
        case 3:{
            system("cls");
            char *p2;
            p2 = &a[usecount].District[0];
            district(a , usecount , p2);
            break;
        }
        case 4:{
			break;
		}
        case 5:{
            system("cls");
            char *p3;
            p3 = a[usecount].mailbox;
            Mailbox(p3);
            break;
        	}
        	break;
        }
}


int Judgement(char password1[] , char password2[]){
    int i,j;
    i = j = 0;
    while(password1[i] != '\0' && password2[j] != '\0'){
        if (password1[i] != password2[j]){
            return 0;
        }
        i++;
        j++;
    }
    if (password1[i] != '\0' || password2[j] != '\0'){
        return 0;
    }
    return 1;
}

// 将整数转换为字符串存储到ID中
void Turn (int *prt , char id[]){
    int i = 0;
    while (*prt != 0){
        id[i] = *prt % 10 + '0';
        *prt = *prt / 10;
        i++;
    }
    id[i] = '\0';
    int left = 0, right = i - 1;
    for (; left < right; left++, right--) {
        char temp = id[left];
        id[left] = id[right];
        id[right] = temp;
    }
}

// 修改字符串内容
void Change (char *name2 , char *prt){
    strcpy(name2, prt);
}

// 选择用户所在地区
void district(U a[] , int usecount , char *p2){
    const char *A[] = {"安徽省" , "澳门"};
    const char *A_city[][10] = {{"合肥市", "芜湖市", "蚌埠市", "淮南市", "马鞍山市", "淮北市", "铜陵市", "安庆市", "黄山市", "阜阳市"} ,
	{"澳门"}};
    const char *B[] = {"北京市"};
    const char *B_city[][10] = {{"东城区", "西城区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区"}};
    const char *C[] = {"重庆市"};
    const char *C_city[][10] = {{"万州区", "涪陵区", "渝中区", "大渡口区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "北碚区", "綦江区"}};
    const char *F[] = {"福建省"};
    const char *F_city[][10] = {{"福州市", "厦门市", "莆田市", "三明市", "泉州市", "漳州市", "南平市", "龙岩市", "宁德市"}};
    const char *G[] = {"甘肃省" , "广东省" , "广西壮族自治区" , "贵州省"};
    const char *G_city[][10] = {{"兰州市", "嘉峪关市", "金昌市", "白银市", "天水市", "武威市", "张掖市", "平凉市", "酒泉市", "庆阳市"} , 
	{"广州市", "韶关市", "深圳市", "珠海市", "汕头市", "佛山市", "江门市", "湛江市", "茂名市", "肇庆市"} ,
	{"南宁市", "柳州市", "桂林市", "梧州市", "北海市", "防城港市", "钦州市", "贵港市", "玉林市", "百色市"} ,
	{"贵阳市", "六盘水市", "遵义市", "安顺市", "毕节市", "铜仁市", "黔西南布依族苗族自治州", "黔东南苗族侗族自治州", "黔南布依族苗族自治州"}};
    const char *H[] = {"海南省" , "河北省" , "河南省" , "黑龙江省" , "湖北省" , "湖南省"};
    const char *H_city[][10] = {{"海口市", "三亚市", "三沙市", "儋州市", "五指山市", "琼海市", "文昌市", "万宁市", "东方市"} ,
	{"石家庄市", "唐山市", "秦皇岛市", "邯郸市", "邢台市", "保定市", "张家口市", "承德市", "沧州市", "廊坊市"} ,
	{"郑州市", "开封市", "洛阳市", "平顶山市", "安阳市", "鹤壁市", "新乡市", "焦作市", "濮阳市", "许昌市"} ,
	{"哈尔滨市", "齐齐哈尔市", "鸡西市", "鹤岗市", "双鸭山市", "大庆市", "伊春市", "佳木斯市", "七台河市", "牡丹江市"} ,
	{"武汉市", "黄石市", "十堰市", "宜昌市", "襄阳市", "鄂州市", "荆门市", "孝感市", "荆州市", "黄冈市"} ,
	{"长沙市", "株洲市", "湘潭市", "衡阳市", "邵阳市", "岳阳市", "常德市", "张家界市", "益阳市", "郴州市"}};
    const char *J[] = {"吉林省" , "江苏省" , "江西省"};
    const char *J_city[][10] = {{"长春市", "吉林市", "四平市", "辽源市", "通化市", "白山市", "松原市", "白城市", "延边朝鲜族自治州"} ,
	{"南京市", "无锡市", "徐州市", "常州市", "苏州市", "南通市", "连云港市", "淮安市", "盐城市", "扬州市"} ,
	{"南昌市", "景德镇市", "萍乡市", "九江市", "新余市", "鹰潭市", "赣州市", "吉安市", "宜春市", "抚州市"}};
    const char *L[] = {"辽宁省"};
    const char *L_city[][10] = {{"沈阳市", "大连市", "鞍山市", "抚顺市", "本溪市", "丹东市", "锦州市", "营口市", "阜新市", "辽阳市"}};
    const char *N[] = {"内蒙古自治区" , "宁夏回族自治区"};
    const char *N_city[][10] = {{"呼和浩特市", "包头市", "乌海市", "赤峰市", "通辽市", "鄂尔多斯市", "呼伦贝尔市", "巴彦淖尔市", "乌兰察布市"} ,
	{"银川市", "石嘴山市", "吴忠市", "固原市", "中卫市"}};
    const char *Q[] = {"青海省"};
    const char *Q_city[][10] = {{"西宁市", "海东市", "海北藏族自治州", "黄南藏族自治州", "海南藏族自治州", "果洛藏族自治州", "玉树藏族自治州", "海西蒙古族藏族自治州"}};
    const char *S[] = {"山东省" , "山西省" , "陕西省" , "上海市" , "四川省"};
    const char *S_city[][10] = {{"济南市", "青岛市", "淄博市", "枣庄市", "东营市", "烟台市", "潍坊市", "济宁市", "泰安市", "威海市"} ,
	{"太原市", "大同市", "阳泉市", "长治市", "晋城市", "朔州市", "晋中市", "运城市", "忻州市", "临汾市"} ,
	{"西安市", "铜川市", "宝鸡市", "咸阳市", "渭南市", "延安市", "汉中市", "榆林市", "安康市", "商洛市"} ,
	{"黄浦区", "徐汇区", "长宁区", "静安区", "普陀区", "虹口区", "杨浦区", "闵行区", "宝山区", "嘉定区"} ,
	{"成都市", "自贡市", "攀枝花市", "泸州市", "德阳市", "绵阳市", "广元市", "遂宁市", "内江市", "乐山市"}};
    const char *T[] = {"天津市" , "台湾省"};
    const char *T_city[][10] = {{"和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "东丽区", "西青区", "津南区", "北辰区"}};
    const char *X[] = {"西藏自治区" , "香港特别行政区" , "新疆维吾尔自治区" , "新疆生产建设兵团"};
    const char *X_city[][10] = {{"拉萨市", "日喀则市", "昌都市", "林芝市", "山南市", "那曲市", "阿里地区"} ,
	{"中西区", "湾仔区", "东区", "南区", "油尖旺区", "深水埗区", "九龙城区", "黄大仙区", "观塘区"} ,
	{"乌鲁木齐市", "克拉玛依市", "吐鲁番市", "哈密市", "阿克苏地区", "喀什地区", "和田地区", "昌吉回族自治州", "博尔塔拉蒙古自治州"}};
    const char *Y[] = {"云南省"};
    const char *Y_city[][10] = {{"昆明市", "曲靖市", "玉溪市", "保山市", "昭通市", "丽江市", "普洱市", "临沧市", "楚雄彝族自治州", "红河哈尼族彝族自治州"}};
    const char *Z[] = {"浙江省"};
    const char *Z_city[][10] = {{"杭州市", "宁波市", "温州市", "嘉兴市", "湖州市", "绍兴市", "金华市", "衢州市", "舟山市", "台州市"}};
    char abc;
    printf ("请输入你的省级行政区首字母(大写)\n");
    scanf ("%c",&abc);
    const char **option;
    const char *(*option_city)[10]; 
    int optioncount = 0;
    int optioncount_city = 0;
    const char *transmit[10];
    const char *transmit1[10][10];
    switch (abc){
        case 'A':
            option = A;
            optioncount = sizeof(A)/sizeof(A[0]);
            option_city = A_city;
            Transport(A , A_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'B':
            option = B;
            optioncount = sizeof(B)/sizeof(B[0]);
            option_city = B_city;
            Transport(B , B_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break; 
        case 'C':
            option = C;
            optioncount = sizeof(C)/sizeof(C[0]);
            option_city = C_city;
            Transport(C , C_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'F':
            option = F;
            optioncount = sizeof(F)/sizeof(F[0]);
            option_city = F_city;
            Transport(F , F_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'G':
            option = G;
            optioncount = sizeof(G)/sizeof(G[0]);
            option_city = G_city;
            Transport(G , G_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'H':
            option = H;
            optioncount = sizeof(H)/sizeof(H[0]);
            option_city = H_city;
            Transport(H , H_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'J':
            option = J;
            optioncount = sizeof(J)/sizeof(J[0]);
            option_city = J_city;
            Transport(J , J_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'L':
            option = L;
            optioncount = sizeof(L)/sizeof(L[0]);
            option_city = L_city;
            Transport(L , L_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'N':
            option = N;
            optioncount = sizeof(N)/sizeof(N[0]);
            option_city = N_city;
            Transport(N , N_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'Q':
            option = Q;
            optioncount = sizeof(Q)/sizeof(Q[0]);
            option_city = Q_city;
            Transport(Q , Q_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'S':
            option = S;
            optioncount = sizeof(S)/sizeof(S[0]);
            option_city = S_city;
            Transport(S , S_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'T':
            option = T;
            optioncount = sizeof(T)/sizeof(T[0]);
            option_city = T_city;
            Transport(T , T_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'X':
            option = X;
            optioncount = sizeof(X)/sizeof(X[0]);
            option_city = X_city;
            Transport(X , X_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'Y':
            option = Y;
            optioncount = sizeof(Y)/sizeof(Y[0]);
            option_city = Y_city;
            Transport(Y , Y_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
        case 'Z':
            option = Z;
            optioncount = sizeof(Z)/sizeof(Z[0]);
            option_city = Z_city;
            Transport(Z , Z_city , transmit , transmit1 , optioncount);
            Select(a , usecount , option , optioncount , p2 , option_city , optioncount_city , transmit , transmit1);
            break;
    } 
    printf ("录入成功"); 
}

void Transport(const char *letter[], const char *letter1[][10], const char *transmit[], const char *transmit1[][10], int optioncount) {
    int province, city;
    for (province = 0; province < optioncount; province++) {
        transmit[province] = letter[province];
        for (city = 0; city < 10; city++) {
            transmit1[province][city] = letter1[province][city];
        }
    }
}


// 选择选项
void Select(U a[], int usecount, const char **option, int optioncount, char *p2, const char *(*option_city)[10], int option_city_count, const char *transmit[], const char *transmit1[][10]) {
    int i, j2;
    for (i = 0; i < optioncount; i++) {
        printf("%d.%s\n", i + 1, option[i]);
    }

    int choice, choice1;
    int flag5 = 0;
    char province[50];

    while (1) {
        if (scanf("%d", &choice) == 1) {
            while (getchar() != '\n');
            if (choice > 0 && choice <= optioncount) {
                snprintf(p2, 50, "%s", option[choice - 1]);
                printf("录入成功");
                flag5 = 1;
                break;
            } else {
                printf("输入错误，请重新输入：");
            }
        } else {
            while (getchar() != '\n');
            printf("输入错误，请输入数字：");
        }
    }

    strcpy(province, p2);

    // 获取城市选项数量
    option_city_count = sizeof(transmit1[0]) / sizeof(transmit1[0][0]); 

    for (j2 = 0; j2 < option_city_count; j2++) {
        printf("%d.%s\n", j2 + 1, transmit1[choice - 1][j2]);
    }

    flag5 = 0;
    while (1) {
        if (scanf("%d", &choice1) == 1) {
            while (getchar() != '\n');
            if (choice1 > 0 && choice1 <= option_city_count) {
                snprintf(p2, 50, "%s", transmit1[choice - 1][choice1 - 1]);
                flag5 = 1;
                break;
            } else {
                printf("输入错误，请重新输入：");
            }
        } else {
            while (getchar() != '\n');
            printf("输入错误，请输入数字：");
        }
    }

    snprintf(a->District, 100, "%s-%s", province, p2);
}


// 选择用户性别
void Option(char *pd){
    char n[] = "男";
    char m[] = "女";
    printf ("请选择你的性别。1.男\n2.女");
    int choose;
    scanf (" %d",&choose);
    while (getchar() != '\n');
    if (choose == 1){
        strcpy(pd , n);
    }else if (choose == 2){
        strcpy(pd , m);
    }
}

// 输入用户生日
void Birthday(char *qd) {
    printf("请输入你的生日：");
    char birthday[20];
    fgets(birthday, sizeof(birthday), stdin);
    birthday[strcspn(birthday, "\n")] = 0;
    if (birthday != NULL) {
        Change(qd, birthday);
    } else {
        perror("读取生日失败");
    }
}

// 输入用户邮箱
void Mailbox(char *p3) {
    printf("请输入你的邮箱：");
    char Emailbox[30];
    fgets(Emailbox, sizeof(Emailbox), stdin);
    Emailbox[strcspn(Emailbox, "\n")] = 0;
    if (Emailbox != NULL) {
        Change(p3, Emailbox);
    } else {
        perror("读取邮箱失败");
    }
}

void WorkManage(U a[], int index) {
    int videoj;
    int comment1;
    int judgementvideo = 0;  // 初始化变量
    for (videoj = 0; videoj < 10; videoj++) {
         if (a[index].video[videoj] != NULL && strcmp(a[index].video[videoj], "") != 0) {
            judgementvideo = 1;  // 找到有内容的视频，标记为有视频作品
            break;
        }
    }
    if (judgementvideo == 1) {
        int work;
        for (work = 0; work < 10; work++) {
            if (a[index].video[work] != NULL && strcmp(a[index].video[work], "") != 0) {
                printf(" %s\n", a[index].video[work]);
                printf("点赞： %d\n", a[index].likework[work]);
                printf("浏览： %d\n", a[index].lookworkcount[work]);
                for(comment1 = 0;comment1 < 100;comment1++){
	                if (a[index].commentarea[work][comment1] != NULL && strcmp(a[index].commentarea[work][comment1], "") != 0){
	                	printf("评论： %s\n", a[index].commentarea[work][comment1]);
	                }
	            }
            }
        }
        printf("隐藏作品\n");
        int work1;
        for (work1 = 0; work1 < 10; work1++) {
            if (a[index].video_hide[work1] != NULL && strcmp(a[index].video_hide[work1], "") != 0) {
                printf(" %s\n", a[index].video_hide[work1]);
            }
        }
    } else {
        printf("没有作品\n");
    }
    int workmanage;
    printf("作品管理\n1.按大小排序\n2.隐藏作品\n3.创作作品\n");
    scanf(" %d", &workmanage);
    while (getchar() != '\n');
    if (workmanage == 1) {
        int check = 0;
        for (check = 0; check < 10; check++) {
            if (a[index].video[check] == NULL || strcmp(a[index].video[check], "") == 0) {
                break;
            }
        }
        if (check == 0) {
            printf("没有作品\n");
            return;
        }
        char *video_copy[10];
        int copy;
        for (copy = 0; copy < 10; copy++) {
            video_copy[copy] = (char *)calloc(100, sizeof(char));
            if (video_copy[copy] == NULL) {
                printf("错误\n");
                int copy1;
                for (copy1 = 0; copy1 < copy; copy1++) {  // 修正循环条件
                    free(video_copy[copy1]);
                }
                return;
            }
            if (a[index].video[copy] != NULL) {
                strcpy(video_copy[copy], a[index].video[copy]);
            }
        }
        int i1, j0;
        for (i1 = 0; i1 < check - 1; i1++) {
            for (j0 = i1 + 1; j0 < check; j0++) {
                if (strlen(video_copy[i1]) > strlen(video_copy[j0])) {
                    char temp[100];
                    strcpy(temp, video_copy[i1]);
                    strcpy(video_copy[i1], video_copy[j0]);
                    strcpy(video_copy[j0], temp);
                }
            }
        }
        int work;
        for (work = 0; work < check; work++) {
            printf(" %s\n", video_copy[work]);
        }
        for (copy = 0; copy < 10; copy++) {
            free(video_copy[copy]);
        }
    } else if (workmanage == 2) {
        int option_w;
        printf("请输入你要隐藏的作品序号\n");
        scanf(" %d", &option_w);
        while (getchar() != '\n');
        int check = 0;
        for (check = 0; check < 10; check++) {
            if (a[index].video[check] == NULL || strcmp(a[index].video[check], "") == 0) {
                break;
            }
        }
        if (option_w >= 0 && option_w < check) {
            int p1;
            for (p1 = 0; p1 < 10; p1++) {
                if (a[index].video_hide[p1] == NULL || strcmp(a[index].video_hide[p1], "") == 0) {
                    break;
                }
            }
            if (p1 < 10) {
                strcpy(a[index].video_hide[p1], a[index].video[option_w]);
                int q1;
                for (q1 = option_w; q1 < check - 1; q1++) {
                    strcpy(a[index].video[q1], a[index].video[q1 + 1]);
                }
                // 清空最后一个视频
                if (check > 0) {
                    free(a[index].video[check - 1]);
                    a[index].video[check - 1] = (char *)calloc(100, sizeof(char));
                }
            }
        }
    } else if (workmanage == 3) {
        printf("是否创作作品\n1.是  2.否\n");
        int workoption;
        scanf(" %d", &workoption);
        while (getchar() != '\n');
        if (workoption == 1) {
            Footage(a[index].video, a, index);
        }
    } else {
        printf("输入错误\n");
        return;
    }
}
// 创作视频作品
void Footage(char *avideo[], U a[], int usecount) {
    char **footage = (char **)calloc(10, sizeof(char *));
    if (footage == NULL) {
        printf("分配失败\n");
        return;
    }

    int i;
    for (i = 0; i < 10; i++) {
        footage[i] = (char *)calloc(100, sizeof(char));
        if (footage[i] == NULL) {
            printf("分配失败\n");
            int l;
            for (l = 0; l < i; l++) {
                free(footage[l]);
            }
            free(footage);
            return;
        }
    }

    printf("请创作你的作品:\n");
    if (fgets(footage[0], 100, stdin) == NULL) {
        printf("输入错误\n");
        for (i = 0; i < 10; i++) {
            free(footage[i]);
        }
        free(footage);
        return;
    }
    footage[0][strcspn(footage[0], "\n")] = 0;

    int k;
    int judgement_work = 1;
    for (k = 0; k < 10; k++) {
        judgement_work = strcmp(a[usecount].video[k],"");
        if(judgement_work == 0){
			break;
		}
    }
    if (k >= 10) {
        printf("错误\n");
        for (i = 0; i < 10; i++) {
            free(footage[i]);
        }
        free(footage);
        return;
    }

    strcpy(a[usecount].video[k], footage[0]);
    printf("创作成功\n");
    a[usecount].worksize[k] = strlen(avideo[k]);

    for (i = 0; i < 10; i++) {
        free(footage[i]);
    }
    free(footage);
}
// 递归展示视频并处理交互
void Recursive(U a[],int count ,int index ,int a12 ,int usecount ,int *nuberoflike ,int *Like1 ,int *follow2 ,int *fan2 ,int op){
    system("cls");
    if (index > count){
        return;
    }
    if (a[a12].video[index] != NULL) {
	        printf("\n%s\n", a[a12].video[index]);
	} else {
	        printf("\n此视频为空\n");
	}
    a[a12].lookworkcount[index]++;
    if (a[a12].video[index] != NULL) {
	    if (op == 1){
	        Followone1(follow2 ,fan2 , a12 , usecount , a);
	        Like2(nuberoflike ,Like1,a,index,a12);
	        Commentarea(a ,index ,a12);
	    }else if (op == 2){
	        Followone1(follow2 ,fan2 , a12 , usecount , a);
	        Like2(nuberoflike ,Like1,a,index,a12);
	        Commentarea(a ,index ,a12);
	    }
    }
    printf ("是否下一个\n是:输入T  否:输入F\n");
    char Scan[3];
    char Scan1[] = {"T"};
    int u;
    gets(Scan);
    u = Judgement(Scan , Scan1);
    if (u == 1){
        index++;
        Recursive(a ,count ,index ,a12 ,usecount ,nuberoflike ,Like1 ,follow2 ,fan2 ,op);
    }
}

// 处理关注操作
void Followone1(int *follow3, int *fan3, int a12, int usecount, U a[]) {
    int x;
    do {
        printf("是否关注\n1.是  2.否\n");
        if (scanf(" %d", &x) != 1) {
            while (getchar() != '\n'); // 清理缓冲区
            printf("输入错误，请输入数字！\n");
        } else {
            break;
        }
    } while (1);

    if (x == 1) {
        (*follow3)++;
        int i;
        for (i = 0; i < 100; i++) {
            if (strcmp(a[usecount].follow_list[i], "") == 0) {
                break;
            }
        }
        if (i < 100) {
            strcpy(a[usecount].follow_list[i], a[a12].ID);
        }
        (*fan3)++;
        for (i = 0; i < 100; i++) {
            if (strcmp(a[a12].fan_list[i], "") == 0) {
                break;
            }
        }
        if (i < 100) {
            strcpy(a[a12].fan_list[i], a[usecount].ID);
        }
    }
}

// 处理点赞操作
void Like2(int *numberoflike, int *Like, U a[], int index, int a12) {
    int y;
    do {
        printf("是否点赞\n1.是  2.否\n");
        if (scanf(" %d", &y) != 1) {
            while (getchar() != '\n'); // 清理缓冲区
            printf("输入错误，请输入数字！\n");
        } else {
            break;
        }
    } while (1);

    if (y == 1) {
        (*numberoflike)++;
        a[a12].likework[index]++;
        (*Like)++;
    }
}

// 处理评论操作
void Commentarea(U a[], int index, int a12) {
    // 显示已有的评论
    int q;
    for (q = 0; q < 100; q++) {
        if (a[a12].commentarea[index] != NULL && a[a12].commentarea[index][q] != NULL && strcmp(a[a12].commentarea[index][q], "") != 0) {
            printf("%s\n", a[a12].commentarea[index][q]);
        }
    }

    // 询问是否要发表评论
    int z;
    do {
        printf("是否评论\n1.是  2.否\n");
        if (scanf(" %d", &z) != 1) {
            while (getchar() != '\n'); // 清理缓冲区
            printf("输入错误，请输入数字！\n");
        } else {
            break;
        }
    } while (1);
    while (getchar() != '\n'); // 清理缓冲区

    if (z == 1) {
        int p;
        for (p = 0; p < 100; p++) {
            if (a[a12].commentarea[index] != NULL && a[a12].commentarea[index][p] != NULL && strcmp(a[a12].commentarea[index][p], "") == 0) {
                break;
            }
        }

        if (p < 100) {
            // 读取用户输入的评论
            printf("请输入评论内容：\n");
            if (fgets(a[a12].commentarea[index][p], 1000, stdin) != NULL) {
                a[a12].commentarea[index][p][strcspn(a[a12].commentarea[index][p], "\n")] = 0; // 去除换行符
            }
        } else {
            printf("评论区已满，无法发表评论。\n");
        }
    }
}
// 取消关注操作
void Delete(U a[], int *follow, int *fan, int usecount) {
    if (*follow == 0) {
        printf("你没有关注任何人！\n");
        return;
    }
    char Id1[20];
    printf("请输入你要取关人的序号: ");
    int t;
    scanf("%d", &t);

    // 输入验证，确保t在合法范围内
    if (t < 0 || t >= *follow) {
        printf("输入的序号无效！\n");
        return;
    }

    char *follow_list;
    follow_list = a[usecount].follow_list[t];
    strcpy(Id1, follow_list); 
    strcpy(follow_list, "");

    int t1;
    for (t1 = t; t1 < *follow - 1; t1++) { 
        strcpy(a[usecount].follow_list[t1], a[usecount].follow_list[t1 + 1]);
    }
    strcpy(a[usecount].follow_list[*follow - 1], "");
    (*follow)--; 

    int Id2 = 0;
    int j; 
    for (j = 0; j < usecount; j++) {
        if (strcmp(a[j].name, Id1) == 0) {
            Id2 = j;
            break;
        }
    }

    // 检查Id2的有效性
    if (Id2 == 0 || Id2 >= usecount) {
        printf("未找到对应的用户或索引无效！\n");
        return;
    }

    char *fan_list;
    int judgement;
    int i;
    for (i = 0; i < a[Id2].fan; i++) {
        judgement = strcmp(a[Id2].fan_list[i], a[usecount].ID);
        if (judgement == 0) {
            break;
        }
    }
    if (i < a[Id2].fan) {
        fan_list = a[Id2].fan_list[i];
        strcpy(fan_list, "");

        int t2;
        for (t2 = i; t2 < a[Id2].fan - 1; t2++) { 
            strcpy(a[Id2].fan_list[t2], a[Id2].fan_list[t2 + 1]);
        }
        strcpy(a[Id2].fan_list[a[Id2].fan - 1], "");
        a[Id2].fan--; 
    }
}

// 比较关注列表，找出好友
void Compare(U a[], int usecount, int *friends) {
    *friends = 0;
    int i,j,k;
    for ( i = 0; i < usecount; i++) {
        for ( j = 0; j < a[i].foollow; j++) {
            for ( k = 0; k < a[usecount].foollow; k++) {
                if (strcmp(a[i].follow_list[j], a[usecount].follow_list[k]) == 0) {
                    (*friends)++;
                    // 在当前用户(i)的好友列表添加目标用户(usecount)的ID
                    int p;
                    for (p = 0; p < a[i].friends; p++) {
                        if (strcmp(a[i].friiends_list[p], "") == 0) {
                            strcpy(a[i].friiends_list[p], a[usecount].ID);
                            break;
                        }
                    }
                    if (p == a[i].friends) {
                        if (a[i].friends < 100) {
                            strcpy(a[i].friiends_list[a[i].friends], a[usecount].ID);
                            a[i].friends++;
                        }
                    }
                    for (p = 0; p < a[usecount].friends; p++) {
                        if (strcmp(a[usecount].friiends_list[p], "") == 0) {
                            strcpy(a[usecount].friiends_list[p], a[i].ID);
                            break;
                        }
                    }
                    if (p == a[usecount].friends) {
                        if (a[usecount].friends < 100) {
                            strcpy(a[usecount].friiends_list[a[usecount].friends], a[i].ID);
                            a[usecount].friends++;
                        }
                    }
                }
            }
        }
    }
}

// 显示用户信息
void Display(U a[] , int number , int cho){
    int row;
    int line;
    if (cho == 1){
        for (row = 0;row < 100;row++){
            for (line = 0;line < 20;line++){
                printf ("%c\n",a[number].follow_list[row][line]);
            }
        }
    }else if (cho == 2){
        for (row = 0;row < 100;row++){
            for (line = 0;line < 20;line++){
                printf ("%c\n",a[number].fan_list[row][line]);
            }
        }
    }else if (cho == 3){
        for (row = 0;row < 100;row++){
            for (line = 0;line < 20;line++){
                printf ("%c\n",a[number].friiends_list[row][line]);
            }
        }
    }
}

void Find1(U a[] , int usecount){
	char find[20];
    printf ("请输入");
    fgets(find, sizeof(find), stdin);
    find[strcspn(find, "\n")] = 0;
    char *find1;
    int i,j,k;
    int flage6 = 0;
    printf ("相关用户\n");
    for (i = 0;i < usecount;i++){
		k = Judgement(find , a[i].name);
		if (k == 1){
			printf (" %s\n", a[i].name);
		}
	}
	for(i = 0;i < usecount;i++){
		k = Judgement(find , a[i].ID);
		if (k == 1){
			printf (" %s\n", a[i].ID);
		}
	}
	printf ("相关视频\n");
    for (i = 0;i < usecount;i++){
        for (j = 0;j < 10;j++){
            find1 = strstr(a[i].video[j], find);
    		if (find1){
            printf("找到包含关键词的视频：%s\n", a[i].video[j]);
            flage6 = 1;
            break;
			}else{
        		printf("未找到包含关键词的视频\n");
			}
		}
		if (flage6 == 1){
			break;
		}
	}
}

// 从文件中加载用户信息
int LoadUsersFromfile(U a[]) {
    FILE *fp = fopen("D:\\18785\\C\\ystem.txt", "r");
    if (fp == NULL) {
        return 1;
    }
    int usecount;
    fread(&usecount, sizeof(int), 1, fp);
    int i, j, k;
    for (i = 0; i < usecount; i++) {
        fread(a[i].name, sizeof(a[i].name), 1, fp);
        fread(a[i].Password, sizeof(a[i].Password), 1, fp);
        fread(a[i].ID, sizeof(a[i].ID), 1, fp);
        fread(a[i].Profile, sizeof(a[i].Profile), 1, fp);
        fread(a[i].Sex, sizeof(a[i].Sex), 1, fp);
        fread(a[i].NatalDay, sizeof(a[i].NatalDay), 1, fp);
        fread(a[i].District, sizeof(a[i].District), 1, fp);
        fread(a[i].School, sizeof(a[i].School), 1, fp);
        fread(a[i].mailbox, sizeof(a[i].mailbox), 1, fp);

        // 读取 video 数组
        for (j = 0; j < 100; j++) {
            size_t len;
            fread(&len, sizeof(size_t), 1, fp);
            if (len > 0) {
                a[i].video[j] = (char *)malloc(len);
                fread(a[i].video[j], len, 1, fp);
            } else {
                a[i].video[j] = NULL;
            }
        }

        // 读取 video_hide 数组
        for (j = 0; j < 100; j++) {
            size_t len;
            fread(&len, sizeof(size_t), 1, fp);
            if (len > 0) {
                a[i].video_hide[j] = (char *)malloc(len);
                fread(a[i].video_hide[j], len, 1, fp);
            } else {
                a[i].video_hide[j] = NULL;
            }
        }

        // 分配 commentarea 内存
        a[i].commentarea = (char ***)malloc(10 * sizeof(char **));
        for (j = 0; j < 10; j++) {
            a[i].commentarea[j] = (char **)malloc(100 * sizeof(char *));
            for (k = 0; k < 100; k++) {
                size_t len;
                fread(&len, sizeof(size_t), 1, fp);
                if (len > 0) {
                    a[i].commentarea[j][k] = (char *)malloc(len);
                    fread(a[i].commentarea[j][k], len, 1, fp);
                } else {
                    a[i].commentarea[j][k] = NULL;
                }
            }
        }

        // 读取 worksize、likework 和 lookworkcount 数组
        fread(a[i].worksize, sizeof(int), 10, fp);
        fread(a[i].likework, sizeof(int), 10, fp);
        fread(a[i].lookworkcount, sizeof(int), 10, fp);

        fread(&a[i].like, sizeof(int), 1, fp);
        fread(&a[i].foollow, sizeof(int), 1, fp);

        // 读取 follow_list
        for (j = 0; j < 100; j++) {
            fread(a[i].follow_list[j], sizeof(a[i].follow_list[j]), 1, fp);
        }

        fread(&a[i].fan, sizeof(int), 1, fp);

        // 读取 fan_list
        for (j = 0; j < 100; j++) {
            fread(a[i].fan_list[j], sizeof(a[i].fan_list[j]), 1, fp);
        }

        fread(&a[i].friends, sizeof(int), 1, fp);

        // 读取 friiends_list
        for (j = 0; j < 100; j++) {
            fread(a[i].friiends_list[j], sizeof(a[i].friiends_list[j]), 1, fp);
        }

        fread(&a[i].NumberOfLike, sizeof(int), 1, fp);
    }
    fclose(fp);
    return usecount;
}

// 将用户信息保存到文件中
void SaveUsersToFile(U a[], int usecount) {
    FILE *fp = fopen("D:\\18785\\C\\ystem.txt", "a");
    if (fp == NULL) {
        perror("无法打开文件");
        return;
    }
    int j, i, k;
    fwrite(&usecount, sizeof(int), 1, fp);
    for (i = 0; i < usecount; i++) {
        fwrite(a[i].name, sizeof(a[i].name), 1, fp);
        fwrite(a[i].Password, sizeof(a[i].Password), 1, fp);
        fwrite(a[i].ID, sizeof(a[i].ID), 1, fp);
        fwrite(a[i].Profile, sizeof(a[i].Profile), 1, fp);
        fwrite(a[i].Sex, sizeof(a[i].Sex), 1, fp);
        fwrite(a[i].NatalDay, sizeof(a[i].NatalDay), 1, fp);
        fwrite(a[i].District, sizeof(a[i].District), 1, fp);
        fwrite(a[i].School, sizeof(a[i].School), 1, fp);
        fwrite(a[i].mailbox, sizeof(a[i].mailbox), 1, fp);

        // 保存 video 数组
        for (j = 0; j < 100; j++) {
            if (a[i].video[j] != NULL) {
                size_t len = strlen(a[i].video[j]) + 1;
                fwrite(&len, sizeof(size_t), 1, fp);
                fwrite(a[i].video[j], len, 1, fp);
            } else {
                size_t len = 0;
                fwrite(&len, sizeof(size_t), 1, fp);
            }
        }

        // 保存 video_hide 数组
        for (j = 0; j < 100; j++) {
            if (a[i].video_hide[j] != NULL) {
                size_t len = strlen(a[i].video_hide[j]) + 1;
                fwrite(&len, sizeof(size_t), 1, fp);
                fwrite(a[i].video_hide[j], len, 1, fp);
            } else {
                size_t len = 0;
                fwrite(&len, sizeof(size_t), 1, fp);
            }
        }

        // 保存 commentarea
        for (j = 0; j < 10; j++) {
            for (k = 0; k < 100; k++) {
                if (a[i].commentarea[j][k] != NULL) {
                    size_t len = strlen(a[i].commentarea[j][k]) + 1;
                    fwrite(&len, sizeof(size_t), 1, fp);
                    fwrite(a[i].commentarea[j][k], len, 1, fp);
                } else {
                    size_t len = 0;
                    fwrite(&len, sizeof(size_t), 1, fp);
                }
            }
        }

        // 保存 worksize、likework 和 lookworkcount 数组
        fwrite(a[i].worksize, sizeof(int), 10, fp);
        fwrite(a[i].likework, sizeof(int), 10, fp);
        fwrite(a[i].lookworkcount, sizeof(int), 10, fp);

        fwrite(&a[i].like, sizeof(int), 1, fp);
        fwrite(&a[i].foollow, sizeof(int), 1, fp);

        // 保存 follow_list
        for (j = 0; j < 100; j++) {
            fwrite(a[i].follow_list[j], sizeof(a[i].follow_list[j]), 1, fp);
        }

        fwrite(&a[i].fan, sizeof(int), 1, fp);

        // 保存 fan_list
        for (j = 0; j < 100; j++) {
            fwrite(a[i].fan_list[j], sizeof(a[i].fan_list[j]), 1, fp);
        }

        fwrite(&a[i].friends, sizeof(int), 1, fp);

        // 保存 friiends_list
        for (j = 0; j < 100; j++) {
            fwrite(a[i].friiends_list[j], sizeof(a[i].friiends_list[j]), 1, fp);
        }

        fwrite(&a[i].NumberOfLike, sizeof(int), 1, fp);
    }
    fclose(fp);
}
